package com.vo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.common.utils.HttpUtil;
import com.entity.Tickers;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.*;
import java.util.*;
import java.util.stream.Stream;

public class DemoVo {

    public static void main(String[] args) throws IOException {
//        String result = HttpUtil.sendGet("https://data.gateapi.io/api2/1/tickers", null);
//
//            String str="{\"fis_usdt\":{\"result\":\"true\",\"last\":\"2.586\",\"lowestAsk\":\"2.608\",\"highestBid\":\"2.586\",\"percentChange\":\"6.9\",\"baseVolume\":\"759599.94616438\",\"quoteVolume\":\"292385.01706779\",\"high24hr\":\"2.871\",\"low24hr\":\"2.47\"},\"snt_usdt\":{\"result\":\"true\",\"last\":\"0.16769\",\"lowestAsk\":\"0.16847\",\"highestBid\":\"0.16593\",\"percentChange\":\"3.25\",\"baseVolume\":\"157257.04825173\",\"quoteVolume\":\"961870.61726318\",\"high24hr\":\"0.17578\",\"low24hr\":\"0.158\"}}";
//            Map maps = (Map) JSON.parse(result);
//            System.out.println("这个是用JSON类来解析JSON字符串!!!");
//        System.out.println(maps.entrySet().size());
//        for (Object map : maps.entrySet()){
//                System.out.println(((Map.Entry)map).getKey()+"     " + ((Map.Entry)map).getValue());
////                JSON.parseObject(((Map.Entry)map).getValue(), Tickers.class)
//                Tickers tickers = JSONObject.parseObject(((Map.Entry) map).getValue() + "", Tickers.class);
//                System.out.println(tickers.getResult());
//                System.out.println(tickers.getPercentChange());
//                System.out.println(tickers.getHigh24hr());
//                System.out.println(tickers.getHighestBid());
//                System.out.println(tickers.getLast());
//                System.out.println(tickers.getHighestBid());
//                System.out.println(tickers.getQuoteVolume());
//                System.out.println(tickers.getQuoteVolume());
//                System.out.println(tickers.getBaseVolume());
//            }

        BigDecimal trend=new BigDecimal("66").subtract(new BigDecimal("55"));
        BigDecimal zf = trend.divide(new BigDecimal("55"),4, BigDecimal.ROUND_HALF_UP);
        System.out.println(zf);
    }

    public  static  void tcpGetData() throws IOException {
        ServerSocket serverSocket=new ServerSocket(10000);
        Socket accept = serverSocket.accept();//监听客户端连接，对应的生成一个socket对象，获取输入流
        InputStream inputStream = accept.getInputStream();
        byte[] bytes=new byte[1024];
        int read = inputStream.read(bytes);
        String s = new String(bytes, 0, read);
        System.out.println(s);
        inputStream.close();
        serverSocket.close();
    }

    public  static  void tcpSend() throws IOException {
//        Socket socket=new Socket(InetAddress.getByName("192.168.1.3"),10000);
        Socket socket=new Socket("192.168.1.3",10000);
        OutputStream out = socket.getOutputStream();
        out.write("hello".getBytes());
        out.close();
    }


    public static void send() throws IOException {
        byte[] bytes="hello world".getBytes();
        int len=bytes.length;
        InetAddress iaddr=InetAddress.getByName("192.168.1.3");
        int port=10086;

        DatagramSocket dgs=new DatagramSocket();
        //构造一个数据包
        DatagramPacket data =new DatagramPacket(bytes,len,iaddr,port);
        //发送数据
        dgs.send(data);
        dgs.close();
    };

    public static  void getData() throws IOException {
        //创建接收端的Socket对象
        DatagramSocket dgs=new DatagramSocket(10086);
        byte[] bys=new byte[1024];
        //创建数据包，用于接收数据
        DatagramPacket dpdata=new DatagramPacket(bys, bys.length);

        //接收数据
        dgs.receive(dpdata);

        int len=dpdata.getLength();
        //解析数据包
        byte[] data = dpdata.getData();
        System.out.println(new String(data,0,len));
        dgs.close();
    }
}
